I'm going to plot the shot chart for every MVP winner since the 96-97 season (data is not available earlier than that):

In [12]:
import pandas as pd
import NBAapi as nba
import numpy as np
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
from matplotlib.colors import LinearSegmentedColormap
import matplotlib.pyplot as plt
import urllib, cStringIO
from scipy import misc
import seaborn

%matplotlib inline
In [13]:
def seasons_string(start,end):
    '''
    creates a list of NBA seasons from start-end
    '''
    years = np.arange(start,end+1)
    seasons = []
    for year in years:
        string1 = str(year)
        string2 = str(year+1)
        season = '{}-{}'.format(string1,string2[-2:])
        seasons.append(season)
    return seasons

Let's load the list of players from the NBA API:

In [3]:
player_list = nba.player.commonallplayers(currentseason=0) # load a list of NBA players including retired players 

And now let's create a list of names and a list of seasons:

In [15]:
players = ['Malone, Karl',
          'Jordan, Michael',
          'Malone, Karl',
          'O\'Neal, Shaquille', 
          'Iverson, Allen',
          'Duncan, Tim',
          'Duncan, Tim',
          'Garnett, Kevin',
          'Nash, Steve',
          'Nash, Steve',
          'Nowitzki, Dirk',
          'Bryant, Kobe',
          'James, LeBron',
          'James, LeBron',
          'Rose, Derrick',
          'James, LeBron',
          'James, LeBron',
          'Durant, Kevin',
          'Curry, Stephen',
          'Curry, Stephen']
 
# create season string based on the seasons Steph Curry played in the league
seasons = seasons_string(1996,2016)
for season,player in zip(seasons,players):
    print(season + ': ' + player)
1996-97: Malone, Karl
1997-98: Jordan, Michael
1998-99: Malone, Karl
1999-00: O'Neal, Shaquille
2000-01: Iverson, Allen
2001-02: Duncan, Tim
2002-03: Duncan, Tim
2003-04: Garnett, Kevin
2004-05: Nash, Steve
2005-06: Nash, Steve
2006-07: Nowitzki, Dirk
2007-08: Bryant, Kobe
2008-09: James, LeBron
2009-10: James, LeBron
2010-11: Rose, Derrick
2011-12: James, LeBron
2012-13: James, LeBron
2013-14: Durant, Kevin
2014-15: Curry, Stephen
2015-16: Curry, Stephen

And now we can easily create the shot charts following the guidelines from this post:

In [11]:
for season,player in zip(seasons,players):
    player_id = player_list[player_list['DISPLAY_LAST_COMMA_FIRST']== player].PERSON_ID # get players id
    shotchart,leagueavergae = nba.shotchart.shotchartdetail(playerid=player_id,season=season) # get shot chart data from NBA.stats
    nba.plot.grantland_shotchart(shotchart,leagueavergae)
    plt.text(0,38,season,fontsize=20,horizontalalignment='center',verticalalignment='center')


Comments

comments powered by Disqus